package smartclass.data;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class ManagerDataBaseHelper extends SQLiteOpenHelper {
	private static final String DATABASE_NAME = "smartclass.db";
	private static final int VERSION = 1;
	public static final String TEACHER_TABLE = "teacher";
	public static final String STUDENT_TABLE = "student";
	public static final String DIRECTION_TABLE = "direction";
	public static final String DOCUMENT_TABLE = "document";
	public static final String HOMEWORK_TABLE = "homework";
	public static final String TEST_TABLE = "test";
	public static final String TESTSTUDENT_TABLE = "teststudent";
	public static final String TOPIC_TABLE = "topics";

	public ManagerDataBaseHelper(Context context) {
		super(context, DATABASE_NAME, null, VERSION);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL("PRAGMA foreign_keys = ON;");
		String creatTable = "CREATE TABLE " + TEACHER_TABLE + "( "
				+ TableItem.ID + " INTEGER PRIMARY KEY," + TableItem.NAME
				+ " TEXT," + TableItem.USER_NAME + " TEXT,"
				+ TableItem.POSITION + " TEXT," + TableItem.SUBJECT + " TEXT,"
				+ TableItem.ADDRESS + " TEXT," + TableItem.GENDER + " TEXT,"
				+ TableItem.PASS_WORD + " TEXT," + TableItem.PHONE_NUMBER
				+ " TEXT);";
		db.execSQL(creatTable);

		creatTable = "CREATE TABLE " + STUDENT_TABLE + "( " + TableItem.ID
				+ " INTEGER PRIMARY KEY, " + TableItem.NAME + " TEXT,"
				+ TableItem.USER_NAME + " TEXT," + TableItem.CLASS_NAME
				+ " TEXT," + TableItem.ADDRESS + " TEXT," + TableItem.GENDER
				+ " TEXT," + TableItem.PASS_WORD + " TEXT,"
				+ TableItem.PHONE_NUMBER + " TEXT);";
		db.execSQL(creatTable);

		creatTable = "CREATE TABLE " + TESTSTUDENT_TABLE + "( " + TableItem.ID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + TableItem.NAME
				+ " TEXT," + TableItem.ID_HOC_SINH + " INTEGER NOT NULL , "
				+ TableItem.ID_GIAO_VIEN + " INTEGER NOT NULL , "
				+ TableItem.PATH + " TEXT," + "FOREIGN KEY ("
				+ TableItem.ID_HOC_SINH + ") REFERENCES " + STUDENT_TABLE
				+ " (" + TableItem.ID + "), " + " FOREIGN KEY ("
				+ TableItem.ID_GIAO_VIEN + ") REFERENCES " + TEACHER_TABLE
				+ " (" + TableItem.ID + "));";
		db.execSQL(creatTable);

		creatTable = "CREATE TABLE " + TOPIC_TABLE + "( " + TableItem.ID
				+ " INTEGER PRIMARY KEY , " + TableItem.NAME + " TEXT,"
				+ TableItem.DATE_CREAT + " TEXT," + TableItem.PATH + " TEXT,"
				+ TableItem.ID_GIAO_VIEN + " INTEGER NOT NULL , FOREIGN KEY ("
				+ TableItem.ID_GIAO_VIEN + ") REFERENCES " + TEACHER_TABLE
				+ " (" + TableItem.ID + "));";
		db.execSQL(creatTable);

		creatTable = "CREATE TABLE " + DOCUMENT_TABLE + "( " + TableItem.ID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + TableItem.NAME
				+ " TEXT," + TableItem.USER_NAME + " TEXT,"
				+ TableItem.DATE_CREAT + " TEXT," + TableItem.ID_TOPIC
				+ " INTEGER NOT NULL ," + TableItem.PATH + " TEXT,"
				+ TableItem.DOCUMENT_INFO + " TEXT, " + " FOREIGN KEY("
				+ TableItem.ID_TOPIC + ") REFERENCES " + TOPIC_TABLE + " ("
				+ TableItem.ID + "));";
		db.execSQL(creatTable);

		creatTable = "CREATE TABLE " + HOMEWORK_TABLE + "( " + TableItem.ID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + TableItem.NAME
				+ " TEXT," + TableItem.DATE_CREAT + " TEXT,"
				+ TableItem.ID_TOPIC + " INTEGER NOT NULL," + TableItem.PATH
				+ " TEXT," + TableItem.HOMEWORK_INFO + " TEXT,"
				+ " FOREIGN KEY(" + TableItem.ID_TOPIC + ") REFERENCES "
				+ TOPIC_TABLE + " (" + TableItem.ID + "));";
		db.execSQL(creatTable);

		creatTable = "CREATE TABLE " + DIRECTION_TABLE + "( " + TableItem.ID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + TableItem.NAME
				+ " TEXT," + TableItem.DATE_CREAT + " TEXT,"
				+ TableItem.ID_HOMEWORK + " INTEGER NOT NULL," + TableItem.PATH
				+ " TEXT," + " FOREIGN KEY(" + TableItem.ID_HOMEWORK
				+ ") REFERENCES " + HOMEWORK_TABLE + " (" + TableItem.ID
				+ "));";
		db.execSQL(creatTable);

		creatTable = "CREATE TABLE " + TEST_TABLE + "( " + TableItem.ID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + TableItem.NAME
				+ " TEXT," + TableItem.DATE_CREAT + " TEXT," + TableItem.TYPE
				+ " TEXT," + TableItem.PATH + " TEXT," + TableItem.ID_GIAO_VIEN
				+ " INTEGER NOT NULL , FOREIGN KEY (" + TableItem.ID_GIAO_VIEN
				+ ") REFERENCES " + TEACHER_TABLE + " (" + TableItem.ID + "));";
		db.execSQL(creatTable);

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL("DROP TABLE IF EXISTS " + TEST_TABLE);
		db.execSQL("DROP TABLE IF EXISTS " + TESTSTUDENT_TABLE);
		db.execSQL("DROP TABLE IF EXISTS " + TEACHER_TABLE);
		db.execSQL("DROP TABLE IF EXISTS " + STUDENT_TABLE);
		db.execSQL("DROP TABLE IF EXISTS " + TOPIC_TABLE);
		db.execSQL("DROP TABLE IF EXISTS " + DOCUMENT_TABLE);
		db.execSQL("DROP TABLE IF EXISTS " + HOMEWORK_TABLE);
		db.execSQL("DROP TABLE IF EXISTS " + DIRECTION_TABLE);

		onCreate(db);
	}

}
